//
//  weatherRequest.swift
//  PerfectTemplate
//
//  Created by Master on 2017/1/19.
//
//

import Foundation
import PerfectLib
import PerfectHTTP

class weatherR: NSObject {
    func getAreaID(_ request: HTTPRequest, response: HTTPResponse) -> Void {
       let area = request.param(name:"area")

        //获取当前时间
        let now = NSDate()
        // 创建一个日期格式器
        let dformatter = DateFormatter()
        dformatter.dateFormat = "yyyyMMddHHmmss"
        let timeStr = dformatter.string(from: now as Date)
        
        let secretString = "b857d5192c074b109d2df831182c2e05"

        let test = baseRequest()
        let hostName = "https://route.showapi.com/9-3"
        let joinString = String(format:"area%@showapi_appid%@showapi_timestamp%@",arguments:[area!,"25067",timeStr])
        let signString = String(format:"%@%@",arguments:[joinString,secretString])
        let md5String = signString.md5()
        let urlString = String(format:"%@?area=%@&showapi_appid=25067&showapi_timestamp=%@&showapi_sign=%@",arguments:[hostName,area!,timeStr,md5String])
        test.getData(urlString: urlString, success: { (responseData) in
//            print("respondData==\(responseData)")
            let dic:[String : Any] = responseData as! [String : Any]
            //数据优化，去除返回数据的无关逻辑
            let code:Int = dic["showapi_res_code"] as! Int
            if code==0 {
                let body:[String:Any] = dic["showapi_res_body"] as! [String : Any]
                let ret_Code:Int = body["ret_code"] as! Int
                if ret_Code==0{
                    let scoreArray: [String:Any] = ["result": "success", "resultCode": 1, "data": body["list"]!
                    ]
                    do {
                        try response.setBody(json: scoreArray)
                    } catch  {
                        print("fail")
                    }
                }else{
                    let scoreArray: [String:Any] = ["result": body["remark"]!, "resultCode": 0, "data": ""
                    ]
                    do {
                        try response.setBody(json: scoreArray)
                    } catch  {
                        print("fail")
                    }
                }
                response.completed()
            }else{
                
                let scoreArray: [String:Any] = ["result": dic["showapi_res_error"]!, "resultCode": 0, "data": ""
                ]
                do {
                    try response.setBody(json: scoreArray)
                } catch  {
                    print("fail")
                }
                
            }
            response.completed()
        }) { (error) in
           print("error==\(error)")
        }
        
        
    }
    func getCityWeather(_ request: HTTPRequest, response: HTTPResponse) -> Void {
        let areaID = request.param(name:"areaId")
        let area = request.param(name:"area")
        //获取当前时间
        let now = NSDate()
        // 创建一个日期格式器
        let dformatter = DateFormatter()
        dformatter.dateFormat = "yyyyMMddHHmmss"
        let timeStr = dformatter.string(from: now as Date)
        
        let secretString = "b857d5192c074b109d2df831182c2e05"
        
        let test = baseRequest()
        let hostName = "https://route.showapi.com/9-2"
        let joinString = String(format:"area%@areaid%@need3HourForcast%@needAlarm%@needHourData%@needIndex%@needMoreDay%@showapi_appid%@showapi_timestamp%@",arguments:[area!,areaID!,"0","0","0","0","1","25067",timeStr])
        let signString = String(format:"%@%@",arguments:[joinString,secretString])
        let md5String = signString.md5()
        let urlString = String(format:"%@?area=%@&areaid=%@&need3HourForcast=%@&needAlarm=%@&needHourData=%@&needIndex=%@&needMoreDay=%@&showapi_appid=%@&showapi_timestamp=%@&showapi_sign=%@",arguments:[hostName,area!,areaID!,"0","0","0","0","1","25067",timeStr,md5String])
//        print("urlString==\(urlString)")
        test.getData(urlString: urlString, success: { (responseData) in
//                        print("respondData==\(responseData)")
            let dic:[String : Any] = responseData as! [String : Any]
            //数据优化，去除返回数据的无关逻辑
            let code:Int = dic["showapi_res_code"] as! Int
            if code==0 {
                let body:[String:Any] = dic["showapi_res_body"] as! [String : Any]
                let ret_Code:Int = body["ret_code"] as! Int
                if ret_Code==0{
                    let reseponseData:[String:Any] = ["cityInfo":body["cityInfo"]!,"now":body["now"]!];
                    let scoreArray: [String:Any] = ["result": "success", "resultCode": 1, "data": reseponseData
                    ]
                    print("scoreArray==\(scoreArray)")
                    do {
                        try response.setBody(json: scoreArray)
                    } catch  {
                        print("fail")
                    }
                }else{
                    let scoreArray: [String:Any] = ["result": body["remark"]!, "resultCode": 0, "data": ""
                    ]
                    do {
                        try response.setBody(json: scoreArray)
                    } catch  {
                        print("fail")
                    }
                }
                response.completed()
            }else{
                
                let scoreArray: [String:Any] = ["result": dic["showapi_res_error"]!, "resultCode": 0, "data": ""
                ]
                do {
                    try response.setBody(json: scoreArray)
                } catch  {
                    print("fail")
                }
                
            }
            response.completed()
        }) { (error) in
            print("error==\(error)")
        }
        
        
    }

}
